package com.margo.project.web.introduction;

import org.aopalliance.aop.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * https://blog.csdn.net/f641385712/article/details/89303088
 * @author fangxingbang
 *
 */
@RequestMapping("/introduction")
@Controller
public class MargoIntroductionController {
	
	Logger logger=LoggerFactory.getLogger(MargoIntroductionController.class);
	
	
	@RequestMapping("/advice")
	@ResponseBody
    public boolean advice() {
        ProxyFactory factory = new ProxyFactory(new MargoGirl());
        factory.setProxyTargetClass(true); // 强制私用CGLIB 以保证我们的Person方法也能正常调用

        // 此处采用IntroductionInterceptor 这个引介增强的拦截器
        Advice advice = new MargoIntroductionInterceptor();

        // 切点+通知（注意：此处放的是复合切面）
        Advisor advisor = new DefaultIntroductionAdvisor((DynamicIntroductionAdvice) advice, IMargoGirl.class);
        //Advisor advisor = new DefaultPointcutAdvisor(cut, advice);
        factory.addAdvisor(advisor);

        IMargoGirl otherInte = (IMargoGirl) factory.getProxy();

        // Person本身自己的方法  也得到了保留
        MargoGirl p = (MargoGirl) factory.getProxy();
        p.run();
        return otherInte.doOther();
    }
}
